{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "4c887845",
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import numpy as np\n",
    "\n",
    "def loadCommentFile(file_name):\n",
    "    all_sentences = []\n",
    "    \n",
    "    with open('comments.csv', 'r', encoding='utf-8') as fp:                                    #读取文件\n",
    "        reader = csv.reader(fp)\n",
    "        \n",
    "        #读取迭代器内的所有评论信息\n",
    "        all_sentences = np.array([[comment[0],comment[1]] for comment in reader],dtype=None)         #保存格式[['评论1','评论情感标签']]         \n",
    "   \n",
    "    print('Step1:read {} comments in file...'.format(len(all_sentences)))\n",
    "    return all_sentences                                                                  #返回numpy数组形式的所有评论内容及对应标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "e99e71ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "def removeSameComment(all_sentences):\n",
    "    \n",
    "    data = pd.DataFrame(all_sentences)\n",
    "    same_sentence_num = data.duplicated().sum()                                           #统计重复的评论内容个数\n",
    "    \n",
    "    if same_sentence_num > 0:\n",
    "        print('Step2:remove {} of same comments...'.format(same_sentence_num))\n",
    "        data = data.drop_duplicates()                                                     #删除重复的评论内容  \n",
    "    \n",
    "    return data.values                                                                   #返回numpy数组形式的评论内容信息\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "18481bc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba\n",
    "import numpy as np\n",
    "def getAllWords(all_sentences):\n",
    "    all_words = []\n",
    "    \n",
    "    for sentence in all_sentences:\n",
    "        words = jieba.lcut(sentence[0])                                                  #将评论切词，并存放所有切分后的评论语句\n",
    "        all_words.append(words)\n",
    "    \n",
    "    print('Step3:jieba cut successfully...')\n",
    "    return np.array(all_words,dtype=object)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "a82c49a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def removeStopWords(file_name, all_words):\n",
    "    stop_words = []\n",
    "    with open('cn-stopwords.txt', 'r', encoding='utf-8') as fp:                      #读取所有停用词\n",
    "        stop_words = fp.read().split('\\n')                                   #存到stop_words列表中(以换行符切分)\n",
    "    \n",
    "    for sentence in all_words:                                              #双重循环去除评论中的停用词\n",
    "        for word in sentence:\n",
    "            if word in stop_words:\n",
    "                sentence.remove(word)\n",
    "    \n",
    "    print('Step4:remove stop-words successfully...')  \n",
    "    return np.array(all_words,dtype=object)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "565e28c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def getDictionary(all_words):\n",
    "    dictionary = []\n",
    "    \n",
    "    for sentence in all_words:\n",
    "        for word in sentence:\n",
    "            \n",
    "            if word not in dictionary:\n",
    "                dictionary.append(word)                                     #将所有评论中出现的词语存入词典\n",
    "    \n",
    "    print('Step5:{} words in total...'.format(len(dictionary)))\n",
    "    return dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "7536c9fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gensim.models import Word2Vec\n",
    "\n",
    "def getWord2Vec(all_words):\n",
    "    \n",
    "    #调用Word2Vec模型，将所有词语信息转化为向量\n",
    "    model = Word2Vec(all_words, sg=0, vector_size=300, window=5, min_count=1, epochs=7, negative=10)\n",
    "    #使用CBOW模型\n",
    "    model.save('word2vec_model')\n",
    "    print('word2vec encoding successfully...')\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "bfe12a32",
   "metadata": {},
   "outputs": [],
   "source": [
    "def getOneHot(dictionary):\n",
    "    one_hots = []\n",
    "    \n",
    "    for index,word in enumerate(dictionary):              #使用one-hot编码把出现的词语转化为向量\n",
    "        one_hot = np.zeros(len(dictionary))\n",
    "        one_hot[index] = 1\n",
    "        \n",
    "        one_hots.append(one_hot)\n",
    "    \n",
    "    print('Step6:one-hot encoding successfully...')\n",
    "    return np.array(one_hots)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "1edaab7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def getData():\n",
    "    #评论的分词形式列表、对应标签、词典\n",
    "    all_sentences = loadCommentFile('comments.csv')\n",
    "    all_sentences = removeSameComment(all_sentences)\n",
    "    target = all_sentences[:,1]\n",
    "    all_words = getAllWords(all_sentences)\n",
    "    all_words = removeStopWords('cn-stopwords.txt', all_words)\n",
    "    dictionary = getDictionary(all_words)\n",
    "    one_hots = getOneHot(dictionary)\n",
    "    \n",
    "    print('get all data successfully...')\n",
    "    \n",
    "    return all_words, target, dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "003b1abd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def getSentenceVec(all_words, word2vec_model):\n",
    "    sentences_vector = []\n",
    "    \n",
    "    for sentence in all_words:\n",
    "        \n",
    "        sentence_vector = np.zeros(word2vec_model.wv.vector_size)\n",
    "        \n",
    "        #取出评论中每个单词的向量累加\n",
    "        for word in sentence:\n",
    "            sentence_vector += word2vec_model.wv.get_vector(word)\n",
    "\n",
    "        #取最终结果的平均值，作为评论语句的向量，并添加到评论向量列表中\n",
    "        sentences_vector.append(sentence_vector/len(sentence))\n",
    "    \n",
    "    #返回numpy类型的评论列表\n",
    "    return np.array(sentences_vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "cae0968f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Step1:read 13999 comments in file...\n",
      "Step2:remove 1017 of same comments...\n",
      "Step3:jieba cut successfully...\n",
      "Step4:remove stop-words successfully...\n",
      "Step5:15977 words in total...\n",
      "Step6:one-hot encoding successfully...\n",
      "get all data successfully...\n",
      "word2vec encoding successfully...\n",
      "train_test_split successfully!\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split         #引入拆分训练集与测试集的方法\n",
    "\n",
    "all_words, target, dictionary = getData()                     #获取评论的分词形式列表、对应标签、词典\n",
    "\n",
    "word2vec_model = getWord2Vec(all_words)                       #训练Word2Vec模型\n",
    "word2vec_model.save('word2vec_model')                         #保存文件\n",
    "\n",
    "#将每一句评论信息转化为对应的评论向量\n",
    "sentences_vector = getSentenceVec(all_words, word2vec_model)\n",
    "\n",
    "#拆分数据集为训练集与测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(sentences_vector, target)\n",
    "print('train_test_split successfully!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "58c73c1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "success\n"
     ]
    }
   ],
   "source": [
    "from sklearn.svm import SVC                                      #引入支持向量机分类器\n",
    "import time\n",
    "svc = SVC(gamma=0.1, C=100)                                       #gamma控制类别的相似度程度，越小越好，C控制正则化程度，适中即可\n",
    "svc.fit(X_train, y_train)                                         #训练支持向量机分类器\n",
    "\n",
    "print('success')\n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9fe540f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "星/14/粉色/真的/少女/超级/喜欢/质量/。/电脑/轻薄/平时/携带/很/方便\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "电脑/起来/不错/流畅/整体/轻薄/磨砂/质感/手感/很棒/日常/办公/足够/，/也/适合/学生/党/。\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "手机/很快/收到/屏幕/太/喜欢/后感/好/尤其/拍照/效果/太好了/双/摄像头/夜拍/很/清晰/，/比/11/强/。/了/一段时间/，/感觉/不错\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "不/满意/也/理解/本着/支持/华为/在/京东/自营/店下/3/天/不到/每本/贬值/100/元/虽然/退/差价/。/不带/鼠标/电脑包/。/不靠/谱/差评\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "不错/不错/做工/其他/轻薄/本好/太多/。/游戏/不能/玩/也/玩/，/屏幕/素质/音响效果/笔记本/里/算/顶级\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "老公/相中/久/电脑/运营/流畅/家里/手机/是/华为/还/互联/很棒\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "太难用/，/不行/。/控制板/不行\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "买回来/星期/降价/两百块/保价/大家/慎重/购买/包装/很/差/就/纸箱装/金/东/越来越/差/以前/买/电器/还/可以/保价/一个月/，/现在/越来越/不行\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "手机/全是/缝隙/imei/手机/显示/少/一位/数字/客服/说/情况/住\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "辣鸡/品控/屏幕/可以/塞进去/发票/纸/系统/会/买\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "挺/垃圾/ /买/看/几个/牌子/ /朋友/反对/买/华为/高开/低配/ /奔/支持/国货/信任/华为/心/买/ /不到/20/天/自动/断网/链接/不上/客服/开始/扯皮/花大/几千/买个/新机/喊/维修/退货/。/真是/诚心/恶心/消费者\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "买/完/降价/客服/说/7/天价/保/第八天/降价/真是/语/不/推荐/京东/自营/店买\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "外观/美丽/小巧/轻薄/携带方便/音质/好/有/指纹/解锁/智能/语音/功能\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "强大/基本/听/不到/风扇/转/，/兼容性问题/很\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "开机/真的/快/颜值/很/高/虽然/评价/为了/京豆/但是/影响/觉得/非常/高颜值/，/用/我/蓝牙/键盘/蓝牙/鼠标/键盘/，/完美\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "轻薄/非常适合/办公/操作/舒服/键盘/顺手/屏幕/个人感觉/舒服/尺寸/我/也/合适/出门/放/背包/里/也/不会/重/，/总体/还是/很/不错/。\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "这破/玩意/支持/刚买/用/一次/黑屏/从/新/启动/还/一问/得/售后/，/刚买/售后/，/破烂货\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "买/两周/降价/搞笑/吧\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "多年/一直/京东/自营/购物/产品/家里/油盐酱醋/米面/青菜/电子产品/手机/是/京东/自营/购买/本次/购买/是/一款/京东/自营/苹果/笔记本/电脑/商品/收到/使用/m1/芯片/确实/很棒/非常/快/，/值得/推荐/，/非常/不错\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "新买/电脑/卡/声音/太/，/玩个/小游戏/嗡嗡/，/买/四五天/降价/两百\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "性能/超级/散热/机箱/小巧/好看/运行/速度/快/噪音/很小/，/看/视频/流畅/清晰/，/很/实用/很/满意\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "买/看/很多/测评/虽然/联想/口碑/不太好/不过/买/到手/真香/别的/型号/了解/yoga/真的/太棒了/机身/质感/小巧/便携/操作/也/流畅/期待/后续/效果/。\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "外包装/挺/外面/一层/京东/物流/包装/是/单独/的/电脑/赫兹/运行/速度/很快/屏幕/效果/挺/的/清晰度/高/散热/性能/挺/的/而且/多种/模式/切换/外出/携带/特别/。\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "买/10/天/跌价/300/元/联系/客服/说/天/保价/还/厉害/保价/周期/算/妥妥/掐/时间/点来/跌价/让/无法/保价/联想公司/素质/真/不怎么样/专坑/消费者/购买/时候/请/谨慎/，/价格/周期/要算/，/希望/消费者/被/坑\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "首发/买到/很快/收到/开机/一看/稳稳/联想/水准/不算/重/女孩子/办公/合适/一/不太会/操作/联系/店铺/小妹/客服/09/咨询/回复/超快/，/态度/特别/用/很丝滑/，/质感/很棒/希望/再有/活动/买/给/家用/。\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "wifi/按钮/没/重启/解决不了/，/正在/上网/课/是/啥事/！/网上/查/是/，/赶紧/个人/我/解决/下/吧/?\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "刚买/星期/降价/二百/还/给/退/差价\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "新/电脑/到货/外观/好看/，/我/喜欢\n",
      "Predict result : 差评\tActual results: 好评\tPredict fail!\t\n",
      "\n",
      "\n",
      "外形/外观/弟弟/喜欢/送给/弟弟/毕业/礼物/这个/蓝色/很/适合/男孩子/待机时间/快充/真的/挺/牛批/真的/很快/不过/充电/头要/买/也/便宜/。/，/我/买/iPhone/图个/好看/?/弟弟/一直/很/喜欢/果果/的/运行/流程/速度/。/所以/618/拿下/。\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "差评/差评/必须/差评/没有/配备/上网/配件\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "颜值/没得说/超薄/轻便/携带/开机/有/指导/注册/很/方便/速度/很快/，/非常/不错\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "Wifi/断/看个/视频/能断/几次/就/出厂/时候/做好/？\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "挺/ /效能/的/高/比/的/2019/ /i9/很多/热量/控制/的/，/电池/很/耐用\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "好/很/适合/女生/很/轻薄/，/键盘/发光/太/适合/女生/！/！/！/！\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "垃圾/买/充电/时候/用卡/要死\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "十三/开箱/十三/香/外壳/薄/封条/纸/撕扯/很/有/设计/感/，/手机/颜色/淡淡的/，/很漂亮/，/找/整机/数据/迁移/办法/，/然后/起来\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "运行/速度/M1/迅速/屏幕/效果/除了/艳丽/艳丽/散热/性能/目前/高负荷/工作/没/听到/风扇/运转/外形/外观/老/外观/不用/评价/轻薄/程度/办公/日常/出差/绝佳/，/其他/特色/运行/速度/方面/提升/，/日常/软件/很/兼容/，/目前/剪/视频/毫无/压力\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "外观/颜值/挺/好看/金属/质感/很/刚来/电脑/运行/速度/很快/，/流畅/，/屏幕显示/效果/不错/多于/学生/算是/性价比/高/客服/很/耐心/指导/激活/office/。\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "感觉/完美/各种/配件/系统/着/丝/滑/舒服/上/一部/电脑/年/这次/换/电脑/实在/太爽了/哈哈哈/6199/价格/感觉/不错/本来/看过/小新/的/联想/ /美帝/良心/ /算了算/了/。/荣耀/是/华为/分家/，/感觉/做工/华为/差太多/，/到手/也/确实/ /很漂亮/！/总之/很/好/很/好/！\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "建议/购买/不管怎么/调/亮度/屏幕/白晃晃/得/眼睛/疼/申请/退货/刚/同意/退回/后来/说/已经/开机/注册/退/我/想/问/开机/用下/，/怎么/是不是/想要/东西/哪怕/亏钱/退/补钱换/更好/，/不/同意/退货/，/请/谨慎/购买\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "运行/速度/非常/流畅/散热/性能/具有/内置/风扇/帮忙/散热\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "购买/收货/花/三天/时间/会/他们/评论/一样/过/拆封/现象/但是/一切/非常/完好/手机/一键/复制/真的/特别/用/很快/复制/好/所有/设置/跟/以前/习惯/一模一样/改变/买/壳子/保护膜/摔/一下/但是/没事/所以/觉得/非常/完美/一次/购物/我/喜欢/银色/但是/没有/银色/货/所以/买/金色/戴上/手机/壳/非常/漂亮/我/给/手机/买/了/全副武装/，/所有/的/地方/保护/了/。\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "运行/速度/速度/开机/运行/很快/屏幕/效果/显示/效果/好/视频/效果/不错/散热/性能/散热/问题/很/凉快/，/外形/外观/外形/简洁/大方\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "月/号/晚上/一直/6/月/号/上午/开始/测试/安装/的/软件/多/但/流畅/界面/简洁/整体/很/舒服/用/三天/没/发现/什么/，/满意度/高/后续/使用/会/追评\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "27/号/活动/结束/抢/，/一下子/抢/。/发货/，/30/号/就/收到/了/！/颜色/好看/，/质感/比/11/，/感受/iPhone/差不多/很/喜欢\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "12/代/处理器/快到/飞起/我/做/程序开发/一个/挺/吃/内存/行业/12/代/CPU/帮助/运行/速度/拉满/再/16g/运行/内存/更是/说/开机/速度/可以/然后/键盘/细节/更是/满满/几乎/能/用到/的/功能/难/搞定/还有/2.5/k/的/雾/面屏/让/办公/追剧/是/舒服/的/一批/还有/一个/就是/硬盘/传输速度/，/能/在/300M/，/拉满\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "物流/很快/当天/到/起来/舒服/手感/好/开机/速度/的/很快/力/不卡/充满/电/用/个/小时/，/感觉/足够/，/不是/很/重/，/携带方便\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "物流/很快/东西/给/力/性价比/高/包装/结实/东西/保护/值得/好评/质量/挺/好/物流/很快/赞/品质/不错/性价比/高/商品/喜欢/亲/直接/下手/犹豫/精挑细选/久/决定/买/一款/到货/果然/失望/实物/很漂亮/做工/精细/特别/满意/物超所值/非常/满意/期间/小/插曲/，/店家/细致/周到/服务/让/人/感动/，/祝/生意兴隆/，/还会/来/超出/预期/，/质量/很/好/，/做工/精细/，/满意/bull/?/?/&/bull/?/?/?/价格/超/优惠/，/性能/非常/棒/，/完美/的/一次/购物\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "一个/充电器/服/，/都\n",
      "Predict result : 差评\tActual results: 差评\tPredict success!\t\n",
      "\n",
      "\n",
      "手感/很棒/很漂亮/系统/流畅/很/轻薄/屏幕/观感/很/不错/小巧/日常/着/也/方便/推荐/购买/轻度/的话/，/续航/问题\n",
      "Predict result : 好评\tActual results: 好评\tPredict success!\t\n",
      "\n",
      "\n",
      "本次测试预测准确度为: 0.98\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "dic_len = len(dictionary)\n",
    "start = random.randint(100,2400)\n",
    "\n",
    "#从测试集中随机抽取50条数据，准备测试\n",
    "X_data = X_test[start:start+50]\n",
    "y_data = y_test[start:start+50]\n",
    "success_test = 0\n",
    "\n",
    "#对 50 条评论信息进行预测\n",
    "for sequence_index in range(len(X_data)):\n",
    "    \n",
    "    #找到该评论在数组中的位置\n",
    "    loc = np.where((sentences_vector == X_data[sequence_index]).all(axis=1))\n",
    "    \n",
    "    #输出该评论语句\n",
    "    print('/'.join(all_words[loc[0][0]]))\n",
    "    \n",
    "    res = svc.predict([X_data[sequence_index]])                #使用支持向量机进行预测\n",
    "\n",
    "    #0 代表好评， 1代表差评\n",
    "    if res == '0':             \n",
    "        print('Predict result : 好评', end='\\t')              #输出好评\n",
    "    else:              \n",
    "        print('Predict result : 差评', end='\\t')             #否则输出差评\n",
    "    \n",
    "    #实际该评论的结果\n",
    "    if y_data[sequence_index] == '0':\n",
    "        print('Actual results: 好评', end='\\t')\n",
    "    else:   \n",
    "        print('Actual results: 差评', end='\\t')\n",
    "        \n",
    "    #判断是否预测正确\n",
    "    if res == y_data[sequence_index]:\n",
    "        print('Predict success!', end='\\t')\n",
    "        success_test += 1\n",
    "    else:\n",
    "        print('Predict fail!', end='\\t')\n",
    "\n",
    "\n",
    "    print('\\n\\n')\n",
    "\n",
    "print('本次测试预测准确度为: {}'.format(success_test/50))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3ca25a5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e217bfdc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd807b31",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
